<template>
  <!-- 渠道 | 交易情况 -->
  <div class="channel-ranking">
    <!-- <div ref="chart" class="chart-wrap"></div> -->
    <e-charts ref="chart" class="chart-wrap" :options="options"/>
    <div class="data-wrap">
      <div class="rank-title">
        <span class="label">{{data.label}}</span>
        <span class="pagination">
          <span class="arrow left" :disabled="currentPage===1" @click="arrowClickHandler(-1)"></span>
          <span>{{currentPage}}/{{totalPage}}</span>
          <span class="arrow right" :disabled="currentPage>=totalPage" @click="arrowClickHandler(1)"></span>
        </span>
      </div>
      <div class="rank-list">
        <div class="rank-list_item" v-for="(it,i) in data.data.slice((currentPage-1) * pageSize, currentPage*pageSize)" :key="i">
          <span class="order" :class="currentPage===1&&('order-'+i)">{{(currentPage-1)*pageSize+i+1}}</span>
          <span>{{it.name}}</span>
          <span class="value">
            <span class="number">{{fmtNumer(it.number,data.label)}}</span>
            <el-tooltip :content="!isNumber(it.linkRatio)?'暂无数据':`环比值：${it.linkRatio}%`" placement="bottom-start">
              <span class="mark" :style="{visibility: isNumber(it.linkRatio)?'visible':'hidden'}">
                <svg-icon :class="it.linkRatio<0?'down':'up'" :iconClass="it.linkRatio<0?'arrow-down':'arrow-up'" />
              </span>
            </el-tooltip>
          </span>
        </div>
      </div>
    </div>
  </div>
</template>

<script type="text/javascript">
import { numToText, isNumber } from '@/utils/index'
import ECharts from '@/components/Echarts'
import * as echarts from 'echarts/core'
import { PieChart } from 'echarts/charts'
echarts.use([
  PieChart
])
export default {
  name: 'ChannelRanking',
  components: {
    ECharts
  },
  props: {
    data: {
      type: Object,
      required: true
    }
  },
  data () {
    return {
      currentPage: 1,
      pageSize: 7,
      totalPage: 0,
      options: null
    }
  },
  methods: {
    init () {
      this.currentPage = 1
      this.totalPage = Math.ceil(this.data.data.length / this.pageSize)
      this.$nextTick(this.renderChart)
    },
    renderChart () {
      const item = this.data
      const data = []
      const other = {
        value: 0,
        name: '其他',
        list: []
      }
      item.data.forEach((it, index) => {
        const obj = { value: it.number, name: it.name }
        if (index <= 7) {
          data.push(obj)
        } else {
          other.value += it.number
          other.list.push(obj)
        }
      })
      data.push(other)
      this.options = {
        tooltip: {
          trigger: 'item',
          formatter: (params) => {
            let str = ''
            const { percent, data: { name, value, list } } = params
            if (name === other.name) {
              str += `<span>占比：${percent}%</span><br/>`
              list.some((it, i) => {
                if (i < 10) {
                  str += `<span>${it.name}：${this.fmtNumer(it.value, params.seriesName)}</span><br/>`
                } else if (i === 10) {
                  str += '<span>...</span>'
                  return true
                }
              })
            } else {
              str += `
                  <span>${name}</span><br/>
                  <span>${params.seriesName}：${this.fmtNumer(value, params.seriesName)}</span><br/>
                  <span>占比：${percent}%</span>
                  `
            }
            return str
          },
          position: function (point, params, dom, rect, size) {
            return [point[0] + 10, point[1] + 10]
          }
        },
        series: [
          {
            name: item.label,
            type: 'pie',
            radius: ['70%', '90%'],
            label: {
              show: true,
              position: 'center',
              formatter: (params) => {
                const label = params.seriesName
                return `{title|${label}}\n{number|${this.fmtNumer(item.number, label)}}\n{${item.linkRatio < 0 ? 'ratioDown' : 'ratioGrow'}|${this.fmtNumer(item.linkRatio, true)}}`
              },
              rich: {
                title: {
                  color: '#999',
                  fontSize: 16
                },
                number: {
                  color: '#666',
                  fontSize: 18,
                  lineHeight: 40
                },
                ratioDown: {
                  color: '#fc6772',
                  fontSize: 14
                },
                ratioGrow: {
                  color: '#2dca93',
                  fontSize: 14
                }
              }
            },
            labelLine: {
              show: false
            },
            data
          }
        ]
      }
    },
    // 渠道详情
    // detail ({ value, label }) {
    //   this.$emit('detail', {
    //     indicator: this.data.value + '',
    //     indicatorName: this.data.label,
    //     union_id: value,
    //     union_name: label
    //   })
    // },
    arrowClickHandler (step) {
      const nextPage = this.currentPage + step
      if (nextPage < 1 || nextPage > this.totalPage) return
      this.currentPage = nextPage
    },
    isNumber (val) {
      return isNumber(val)
    },
    fmtNumer (val, label = '') {
      if (!isNumber(val)) return '-'
      if (label === true || (typeof label === 'string' && label.includes('率'))) {
        val = val + '%'
      } else {
        val = numToText(val)
      }
      return val
    }
  },
  watch: {
    data: {
      handler () {
        this.init()
      },
      immediate: true
    }
  }
}
</script>

<style lang="scss" scoped>
.channel-ranking {
  padding-top: 10px;
  .data-wrap {
    padding: 0 10px;
    border-right: 1px dashed #ddd;
  }
  .chart-wrap {
    width: 200px;
    height: 200px;
    margin: 0 auto;
  }
  .rank-title {
    margin: 20px 0;
    .label {
      font-size: 16px;
      font-weight: bold;
    }
    .pagination {
      display: inline-flex;
      align-items: center;
      margin-left: 16px;
      .arrow {
        cursor: pointer;
        border-width: 8px;
        border-style: solid;
        &[disabled='disabled'] {
          cursor: not-allowed;
          opacity: 0.5;
        }
        &.left {
          margin-right: 6px;
          border-color: transparent #333 transparent transparent;
        }
        &.right {
          margin-left: 6px;
          border-color: transparent transparent transparent #333;
        }
      }
    }
  }
  .rank-list {
    height: 252px;
    .rank-list_item {
      display: flex;
      color: #333;
      margin-bottom: 14px;
      align-items: center;
      .order {
        width: 24px;
        height: 24px;
        line-height: 24px;
        text-align: center;
        border-radius: 50%;
        background-color: #eee;
        &.order-0 {
          color: #fff;
          background-color: #f59a23;
        }
        &.order-1 {
          color: #fff;
          background-color: #aaa;
        }
        &.order-2 {
          color: #fff;
          background-color: #c48f5b;
        }
      }
      .label {
        cursor: pointer;
        margin: 0 20px;
        &:hover {
          color: #409eff;
        }
      }
      .value {
        flex: 1;
        text-align: right;
      }
      .mark {
        .down {
          color: #fc6772;
        }
        .up {
          color: #2dca93;
        }
      }
    }
  }
}
</style>
